/**
 * 
 */
package com.mobile.ado.setting.calendar;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author kennyd
 * 
 */
public class CalendarView extends View {

	private int leftPadding = 20;
	private int topPadding = 100;

	private int columnPadding;
	private int rowPadding;

	private Calendar calendar;
	private Calendar today;

	private final Paint todayPaint;
	private final Paint normalPaint;
	private ArrayList<DateRec> rects;

	private GestureDetector gestureDetector;

	public CalendarView(Context context, int backGround) {
		super(context);

		calendar = Calendar.getInstance();
		today = (Calendar) calendar.clone();

		todayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		todayPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		todayPaint.setColor(Color.RED);
		todayPaint.setTextSize(20f);

		normalPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		normalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		normalPaint.setColor(Color.BLACK);
		normalPaint.setTextSize(20f);

		rects = new ArrayList<DateRec>();

		CalendarGestureListener listener = new CalendarGestureListener(
				(Activity) context, this, calendar, rects);

		gestureDetector = new GestureDetector(listener);

		setBackgroundResource(backGround);
	}

	@Override
	protected void onSizeChanged(int width, int height, int oldWidth,
			int oldHeight) {
		super.onSizeChanged(width, height, oldWidth, oldHeight);
		columnPadding = (width - leftPadding) / 7;
		rowPadding = (height - topPadding) / 7;

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		drawYearMonthLine(canvas);
		drawWeekLine(canvas);
		drawDaysLine(canvas);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gestureDetector.onTouchEvent(event);
		return true;
	}

	private void drawDaysLine(Canvas canvas) {
		calendar.set(Calendar.DATE, 1);
		int month = calendar.get(Calendar.MONTH);
		int y = 1;
		while (calendar.get(Calendar.MONTH) == month) {
			int day = calendar.get(Calendar.DATE);
			int x = calendar.get(Calendar.DAY_OF_WEEK);

			if (x == 1) {
				x = 7;
			} else {
				x = x - 1;
			}
			int dayX = leftPadding + (x - 1) * columnPadding;
			int dayY = topPadding + y * rowPadding;
			rects.add(new DateRec(dayX, dayY, day));

			if (calendar.equals(today)) {
				canvas.drawText(day + "", dayX, dayY, todayPaint);
			} else {
				canvas.drawText(day + "", dayX, dayY, normalPaint);
			}

			if (x == 7) {
				y = y + 1;
			}
			calendar.add(Calendar.DATE, 1);
		}
		calendar.add(Calendar.DATE, -1);
	}

	private void drawWeekLine(Canvas canvas) {
		String[] weekString = new String[] { "一", "二", "三", "四", "五", "六", "日" };

		for (int i = 0; i < weekString.length; i++) {
			canvas.drawText(weekString[i], leftPadding + i * columnPadding,
					topPadding, normalPaint);
		}
	}

	private void drawYearMonthLine(Canvas canvas) {
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		String yearMonthLine = year + "年" + (month + 1) + "月";
		canvas.drawText(yearMonthLine, (columnPadding * 7) / 2 - 35, 50,
				normalPaint);
	}
}
